<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>facade模式 &mdash; programming v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="top" title="programming v0.1 documentation" href="../index.html" />
    <link rel="up" title="设计模式学习笔记" href="index.html" />
    <link rel="next" title="template模式" href="template.html" />
    <link rel="prev" title="adapter模式" href="adapter.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="template.html" title="template模式"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="adapter.html" title="adapter模式"
             accesskey="P">上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="facade">
<h1>facade模式<a class="headerlink" href="#facade" title="永久链接至标题">¶</a></h1>
<div class="section" id="id1">
<span id="index-5"></span><span id="id2"></span><h2>facade模式定义<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p><strong>The Facade Pattern</strong> provides a unified interface to a set of interfaces
in a subsystem. Facade defines a higher-level interface that makes the
subsystem easier to use.</p>
<p><strong>Facade模式</strong> 为一个子系统的一系列接口提供一个统一的接口.
Facade定义了使得子系统更容易使用的高级接口.</p>
</div>
<div class="section" id="id3">
<h2>问题描述<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>家庭影院系统包括:功放,投影仪,DVD播放器等,
那么每次欣赏电影时,可以使用各自类的方法来完成,如,先打开投影仪,然后DVD播放器,然后
投影仪,然后功放等,欣赏完成后以相反的顺序关闭.但是这样显然比较麻烦,此时可使用
<strong>Facade模式</strong> 提供简单的接口,即播放电影和关闭电影.</p>
<p>Facade模式的类图:</p>
<img alt="../_images/facade_class.png" src="../_images/facade_class.png" />
</div>
<div class="section" id="id4">
<h2>具体代码<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>下载请点击 <a href="../_downloads/facade.cpp"><strong class="xref">这里</strong></a></p>
<div class="highlight-c++"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76</pre></td><td class="code"><div class="highlight"><pre><span class="cp">#include&lt;iostream&gt;</span>
<span class="cp">#include&lt;string&gt;</span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Device</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">string</span> <span class="n">name</span><span class="p">;</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">on</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="n">name</span><span class="o">&lt;&lt;</span><span class="s">&quot; is on.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">play</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="n">name</span><span class="o">&lt;&lt;</span><span class="s">&quot; is playing.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">off</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="n">name</span><span class="o">&lt;&lt;</span><span class="s">&quot; is off.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">DVD</span><span class="o">:</span><span class="k">public</span> <span class="n">Device</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">DVD</span><span class="p">(){</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;DVD&quot;</span><span class="p">;}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Amplifier</span><span class="o">:</span><span class="k">public</span> <span class="n">Device</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Amplifier</span><span class="p">(){</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;Amplifier&quot;</span><span class="p">;}</span>
<span class="p">};</span>
<span class="k">class</span> <span class="nc">Projector</span><span class="o">:</span><span class="k">public</span> <span class="n">Device</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Projector</span><span class="p">(){</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;Projector&quot;</span><span class="p">;}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">FacadeThreater</span>
<span class="p">{</span>
    <span class="k">private</span><span class="o">:</span>
        <span class="n">DVD</span><span class="o">*</span> <span class="n">dvd</span><span class="p">;</span>
        <span class="n">Amplifier</span><span class="o">*</span> <span class="n">amplifier</span><span class="p">;</span>
        <span class="n">Projector</span><span class="o">*</span> <span class="n">projector</span><span class="p">;</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">FacadeThreater</span><span class="p">(</span><span class="n">DVD</span> <span class="o">*</span><span class="n">dvd</span><span class="p">,</span> <span class="n">Amplifier</span> <span class="o">*</span><span class="n">amplifier</span><span class="p">,</span> <span class="n">Projector</span> <span class="o">*</span><span class="n">projector</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">this</span><span class="o">-&gt;</span><span class="n">dvd</span> <span class="o">=</span> <span class="n">dvd</span><span class="p">;</span>
            <span class="k">this</span><span class="o">-&gt;</span><span class="n">amplifier</span> <span class="o">=</span> <span class="n">amplifier</span><span class="p">;</span>
            <span class="k">this</span><span class="o">-&gt;</span><span class="n">projector</span> <span class="o">=</span> <span class="n">projector</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="kt">void</span> <span class="n">playMovie</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">dvd</span><span class="o">-&gt;</span><span class="n">on</span><span class="p">();</span>
            <span class="n">projector</span><span class="o">-&gt;</span><span class="n">on</span><span class="p">();</span>
            <span class="n">amplifier</span><span class="o">-&gt;</span><span class="n">on</span><span class="p">();</span>
            <span class="n">dvd</span><span class="o">-&gt;</span><span class="n">play</span><span class="p">();</span>
        <span class="p">}</span>
        <span class="kt">void</span> <span class="n">stopMovie</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">amplifier</span><span class="o">-&gt;</span><span class="n">off</span><span class="p">();</span>
            <span class="n">projector</span><span class="o">-&gt;</span><span class="n">off</span><span class="p">();</span>
            <span class="n">dvd</span><span class="o">-&gt;</span><span class="n">off</span><span class="p">();</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">DVD</span> <span class="n">dvd</span><span class="p">;</span>
    <span class="n">Amplifier</span> <span class="n">amplifier</span><span class="p">;</span>
    <span class="n">Projector</span> <span class="n">projector</span><span class="p">;</span>
    <span class="c">// non-facade method</span>
    <span class="c">// play movie</span>
    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;################# Non-facade Pattern ####################&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">dvd</span><span class="p">.</span><span class="n">on</span><span class="p">();</span>
    <span class="n">projector</span><span class="p">.</span><span class="n">on</span><span class="p">();</span>
    <span class="n">amplifier</span><span class="p">.</span><span class="n">on</span><span class="p">();</span>
    <span class="n">dvd</span><span class="p">.</span><span class="n">play</span><span class="p">();</span>

    <span class="c">// using facade pattern</span>
    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;################# Facade Pattern ####################&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">FacadeThreater</span> <span class="n">ft</span><span class="p">(</span><span class="o">&amp;</span><span class="n">dvd</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">amplifier</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">projector</span><span class="p">);</span>
    <span class="n">ft</span><span class="p">.</span><span class="n">playMovie</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td></tr></table></div>
<p>输出结果为:</p>
<div class="highlight-python"><pre>################# Non-facade Pattern ####################
DVD is on.
Projector is on.
Amplifier is on.
DVD is playing.
################# Facade Pattern ####################
DVD is on.
Projector is on.
Amplifier is on.
DVD is playing.</pre>
</div>
</div>
<div class="section" id="id5">
<h2>面对新的需要<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
<p>只需要进行相关的封装即可.</p>
</div>
<div class="section" id="id6">
<h2>更多注意<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h2>
<p>Decorator模式, Adapter模式, Facade模式的区别参见 <a class="reference external" href="adapter.html#id1"><em>adapter模式定义</em></a></p>
<p>设计准则:</p>
<p><strong>Principle of Least Knowledge-talk only to your immediate friends.</strong></p>
<p>此准则也称为 <strong>Law of Demeter</strong>, <strong>迪米特准则</strong></p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">米特法则有一些很形象的比喻：
如果你想让你的狗狗跑的话，你会对狗狗说还是对四条狗腿说？
如果你去店里买东西，你会把钱交给店员，还是会把钱包交给店员让他自己拿？</p>
</div>
<p>这个准则告诉我们, 我们只能调用下列情形下的方法:</p>
<ul class="simple">
<li>对象自身</li>
<li>作为参数对象传递传递给方法</li>
<li>方法创建或者实例化的任何对象</li>
<li>任何对象的部件</li>
</ul>
<p>一个遵循此准则的例子:</p>
<img alt="../_images/least_knowledge_exp.png" src="../_images/least_knowledge_exp.png" />
</div>
<div class="section" id="id7">
<h2>参考更多<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
<ol class="arabic simple">
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Facade_pattern">Facade_Pattern</a></li>
</ol>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference external" href="">facade模式</a><ul>
<li><a class="reference external" href="#id1">facade模式定义</a></li>
<li><a class="reference external" href="#id3">问题描述</a></li>
<li><a class="reference external" href="#id4">具体代码</a></li>
<li><a class="reference external" href="#id5">面对新的需要</a></li>
<li><a class="reference external" href="#id6">更多注意</a></li>
<li><a class="reference external" href="#id7">参考更多</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="adapter.html"
                                  title="上一章">adapter模式</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="template.html"
                                  title="下一章">template模式</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/designpattern/facade.txt"
                     rel="nofollow">显示源代码</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>快速搜索</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="搜索" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              输入相关的模块，术语，类或者函数名称进行搜索
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="template.html" title="template模式"
             >下一页</a> |</li>
        <li class="right" >
          <a href="adapter.html" title="adapter模式"
             >上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" >设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; 版权所有 2009, zhutao.iscas@gmail.com.
      使用 <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>